package com.wuh.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wuh.model.Teacher;
import com.wuh.service.TeacherService;

@Controller
public class TeacherController {

	@Autowired
    private TeacherService teacherService;
	
	
	@RequestMapping("/toTeacherLogin")
    public String toTeacherLogin(){
        return  "loginreg/teacher_login";
    }
	
	@RequestMapping("/teacherLogin")
    public String teacherLogin(Teacher teacher, HttpSession session){
        Teacher t =teacherService.login(teacher);
        if (t !=null){
            session.setAttribute("sess_user",t);
            session.setAttribute("loginMessage","teacher");
            String a = t.getLogin_name();
            session.setAttribute("loginname", a);
            return "redirect:/";
        }else{
            session.setAttribute("msg","用户名或密码错误");
            return  "redirect:/toTeacherLogin";
        }
    }

    @RequestMapping("/teacherRegister")
    public  String teacherRegister(Teacher teacher, Model model){
        teacherService.register(teacher);
        
        return  "loginreg/teacher_login";
    }

    @RequestMapping("/toTeacherIndex")
    public String toTeacherIndex(){
        return  "teacher/teacher_index";
    }

	@RequestMapping("/toTeacherInfo")
	public String toTeacherInfo() {

		return "teacher/teacher_info";
	}

	@RequestMapping("/editTeacher")
	public String editTeacher(Teacher teacher,HttpSession session) {

		teacherService.editTeacher(teacher);
		Teacher st = ((Teacher)session.getAttribute("sess_user"));
		Teacher t = teacherService.getTeacherById(st.getTeacher_id());
		session.setAttribute("sess_user", t);

		return "redirect:/toTeacherInfo";
	}

	@RequestMapping("/editTeacherPassword")
	@ResponseBody   //ajax注解
	public int editTeacherPassword(String oldPassword,String newPassword,HttpSession session) {
		int flag = 0;
		Teacher teacher = (Teacher) session.getAttribute("sess_user");
		if(oldPassword.equals(teacher.getPassword())) {
			teacher.setPassword(newPassword);
			flag = teacherService.editPasswordById(teacher);

		}

		return flag;
	}

    @RequestMapping("/teacherUpload")
    public String teacherUpload(MultipartFile upImage,HttpSession session) {

    	Teacher teacher = new Teacher();
    	Teacher sess_user = (Teacher) session.getAttribute("sess_user");
    	teacher.setTeacher_id(sess_user.getTeacher_id());
        System.out.println("修改接收到的teacher：" + teacher);
        if (!upImage.isEmpty()) {
        	//获取项目文件地址
        	String filepath = new String("src/main/resources/"+"static/teacher/img");
        	File staticFile = new File(filepath);
            //设置文件名
        	String fileName = upImage.getOriginalFilename();  //获取原文件名
            String uuid = UUID.randomUUID().toString().replace("-", ""); //把文件的名称设置唯一值，uuid
            fileName = uuid + "_" + fileName; //文件名
            //图片保存全路径
            String uploadPath = staticFile.getAbsolutePath() + File.separator + fileName;
            File targetFile = new File(uploadPath);
            //上传文件

            if (!targetFile.exists()) {
            	targetFile.mkdirs();
            	System.out.println("创建新文件夹");
            }else {
            	System.out.println("文件夹存在");
            }
            try {
                upImage.transferTo(targetFile);  //上传
                System.out.println("上传成功！");
            } catch (IOException e) {
                e.printStackTrace();
            }
            //存储图片访问地址
            fileName = "/teacher\\img\\" + fileName;
            teacher.setImg_url(fileName);
            teacherService.editTeacher(teacher);
            //更新session
            sess_user = teacherService.getTeacherById(sess_user.getTeacher_id());
            session.setAttribute("sess_user",sess_user);
        }
        return "redirect:/toTeacherInfo";
    }

    @RequestMapping("/getTeachers")
	public String getTeachers(Teacher teacher,Model model) {
    	teacher.setPageSize(5);
    	PageInfo<Teacher> pageInfo = teacherService.getTeachers(teacher);
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("schTeacher",teacher);

		return "teacher/teacher_List";
	}

    @GetMapping("/delTeachers")
	public String delTeachers(Integer[] ids) {
    	teacherService.delTeachers(ids);

		return "redirect:/getTeachers";
	}

    @GetMapping("/freezeTeachers")
	public String freezeTeachers(Integer[] ids) {
    	teacherService.freezeTeachers(ids);

		return "redirect:/getTeachers";
	}

    @GetMapping("/unfreezeTeachers")
	public String unfreezeTeachers(Integer[] ids) {
    	teacherService.unfreezeTeachers(ids);

		return "redirect:/getTeachers";
	}

    @RequestMapping("/addTeacher")
    public String addTeacher(Teacher teacher) {
    	teacherService.addTeacher(teacher);

		return "redirect:/getTeachers";
	}
}
